<?php

/**
 * Contact form.
 *
 * @package    form
 * @subpackage contact
 * @version    SVN: $Id: sfPropelFormTemplate.php 6174 2007-11-27 06:22:40Z fabien $
 */
class ContactForm extends BaseContactForm
{
  public function configure()
  {
  
    if($this->isNew())
      $this->validatorSchema['iden'] = new sfValidatorString(array('required' => true));
    $this->widgetSchema['contact_category'] =
      new sfWidgetFormSelect(
        array(
          'choices'=>
          array_combine(sfConfig::get('app_selection_contact_category'),sfConfig::get('app_selection_contact_category')),
        )
      );
    $this->widgetSchema['country_name'] =
      new sfWidgetFormSelect(
        array(
          'choices'=>
          array_combine(ListCountriesPeer::getListWithBlank(),ListCountriesPeer::getListWithBlank()),
        )
      );
  
    $this->widgetSchema->setLabels(array(
      'first_name'       => 'First Name',
      'last_name'        => 'Last Name',
      'middle_name'      => 'Middle Name',
      'company_name'     => 'Company',
      'contact_category' => 'Type',
      'country_name'     => 'Country',
      'name_suffix'      => 'Suffix',
      'company_initials' => 'Initials',
      'cellphone'        => 'Cellphone',
      'pobox'            => 'P.O. Box',
      'telex'            => 'Telex',
      'person_position'  => 'Position',
      'founding_day'  => 'Founding Day',
      'status'=>'Status',
    ));
  

    $this->widgetSchema['company_name']->setAttribute('size',50);

    $founding_years = range(date('Y')-100,date('Y')+1);
    $this->widgetSchema['founding_day']->setOption('years',array_combine($founding_years,$founding_years));
    $birthday_years = range(date('Y')-90,date('Y')+1);
    $this->widgetSchema['birthday']->setOption('years',array_combine($birthday_years,$birthday_years));
    $this->widgetSchema['status']->setAttribute('readonly',true);
    $this->setDefault('status','ACTIVE');
    
    $this->validatorSchema['list_name']->setOption('required',false);
    $this->validatorSchema['middle_name']->setOption('required',false);
    $this->validatorSchema['salutation']->setOption('required',false);
    $this->validatorSchema['name_suffix']->setOption('required',false);
    $this->validatorSchema['person_position']->setOption('required',false);
    $this->validatorSchema['fax']->setOption('required',false);
    $this->validatorSchema['phone']->setOption('required',false);
    $this->validatorSchema['cellphone']->setOption('required',false);
    $this->validatorSchema['pobox']->setOption('required',false);
    $this->validatorSchema['telex']->setOption('required',false);
    $this->validatorSchema['website']->setOption('required',false);
    $this->validatorSchema['email']->setOption('required',false);
    $this->validatorSchema['zip_code']->setOption('required',false);
    $this->validatorSchema['remarks']->setOption('required',false);
    $this->validatorSchema['company_initials']->setOption('required',false);
    
    $this->validatorSchema['last_name']->setOption('required',false);
    $this->validatorSchema['last_name']->setOption('min_length',1);
    $this->validatorSchema['first_name']->setOption('required',false);
    $this->validatorSchema['first_name']->setOption('min_length',2);
    $this->validatorSchema['company_name']->setOption('required',false);
    $this->validatorSchema['company_name']->setOption('min_length',5);
    
    $this->validatorSchema['country_name'] = new sfValidatorChoice(array('choices' => ListCountriesPeer::getList(),));
    
    $this->validatorSchema->setPostValidator(
      new sfValidatorCallback(
        array(
          'callback'=>array(
            $this,'validateContactType',
          ),
        )
      )
    );
    
  }
  
  
  public function validateContactType($validator, $values){
    $contact_category = $values['contact_category'];
    $company_name = $values['company_name'];
    $first_name = $values['first_name'];
    $last_name = $values['last_name'];
    $error = array();
    if($contact_category == 'COMPANY'){
      if(! $company_name)
        $error['company_name'] = new sfValidatorError($validator,'Required');
    }elseif($contact_category == 'PERSON'){
      if (! $first_name)
        $error['first_name']= new sfValidatorError($validator,'Required');
      if (! $last_name)
        $error['last_name']= new sfValidatorError($validator,'Required');
    }elseif($contact_category == 'GOVERNMENT'){
      if(! $company_name)
        $error['company_name']= new sfValidatorError($validator,'Required');
      if (! $first_name)
        $error['first_name']= new sfValidatorError($validator,'Required');
      if (! $last_name)
        $error['last_name']= new sfValidatorError($validator,'Required');
    }
    if(count($error)>0)
      throw new sfValidatorErrorSchema($validator,$error);
      
    return $values;
  }

  public function setIden($c){
    $this->setDefault('iden',$c);
  }
  
  public function getUrl(){
    if ($this->isNew())
      return 'contact/insert';
    else
      return 'contact/update';
  }
  
  public function getResetUrl(){
    if ($this->isNew())
      return 'contact/create';
    else
      return 'contact/edit';
  }
  
  public function getDeleteUrl(){
    return 'contact/delete';
  }
  
  public function getUpdateStatusUrl(){
    return 'contact/updateStatus';
  }
  
  public static function getContactRowId($contact){
    return 'row_contact_'.$contact->getIden();
  }
  
  public function getContactRowUpdateUrl(){
    return 'contact/getColumn?iden='.$this->getObject()->getIden();
  }
  
  public static function getContactLinkId($contact){
    return 'link_select_contact_'.$contact->getIden();
  }
  
}
